package com.ehotting.eaf.core.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class NumberHelper {

	private final static Logger logger = LoggerFactory.getLogger(NumberHelper.class);
	
	
	/**
	 * 数字验证
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isDigital(String str) {
		return str!=null && str.matches("^[0-9]*$");
	}
	
	
	public static boolean isInteger(String s){
		return s!=null && s.matches("[-\\+]?[\\d]*$");
	}

	public static boolean isDouble(String s){
		return s!=null && s.matches("[-\\+]?[.\\d]*$");
	}
	
	public static Double parseDouble(String s){
		
		if(isDouble(s)){
			return Double.parseDouble(s);
		}
		return 0.00;
	}
	
	public static Integer parseInteger(String s){
		if(isInteger(s)){
			return Integer.parseInt(s);
		}
		if(isDouble(s)){
			s = s.substring(0,s.indexOf("."));
			if(isDigital(s)){
				return Integer.parseInt(s);
			}
		}
		return 0;
	}

	public static Long parseLong(String s){
		if(isInteger(s)){
			return Long.parseLong(s);
		}
		if(isDouble(s)){
			s = s.substring(0,s.indexOf("."));
			if(isDigital(s)){
				return Long.parseLong(s);
			}
		}
		logger.error("ParseLong fail,Valid format s:{}",s);
		return 0L;
	}
	
	
	public static String convertFloat2(String s){
		if(s == null || !isDouble(s)){
			return null;
		}
		return String.format("%.2f",NumberHelper.parseDouble(s));
	}
	
	
	public static String convertFloat(String s){
		if(s == null || !isDouble(s)){
			return null;
		}
		return String.format("%.0f",NumberHelper.parseDouble(s));
	}
	
	public static Double convertFloat2(Double s){
		if(s == null ){
			return null;
		}
		return NumberHelper.parseDouble(String.format("%.2f",s));
	}
	
	
	public static void main(String[] args){
		String s = "-90.199999999999";
		System.out.println(isDouble(s));
		System.out.println(NumberHelper.convertFloat(s));
	}
	

}
